using Identity.Application.Contracts.Services;
using Microsoft.EntityFrameworkCore;

namespace Identity.Infrastructure.Services
{
    public class PermissionCenterService : IPermissionCenterService
    {
        private readonly ApplicationDbContext _db;
        public PermissionCenterService(ApplicationDbContext db)
        {
            _db = db;
        }

        public async Task<bool> UserHasPermissionAsync(string userId, string permissionKey)
        {

            // 查询数据库或缓存判断权限
            return await _db.UserRoles
             .Where(ur => ur.UserId.ToString() == userId)
             .Join(_db.RolePermissions,
                   ur => ur.RoleId,
                   rp => rp.RoleId,
                   (ur, rp) => rp)
             .Join(_db.Permissions,
                   rp => rp.PermissionId,
                   p => p.Id,
                   (rp, p) => p)
             .AnyAsync(p => p.Key == permissionKey);
        }
    }
}